﻿using AutoMapper;
using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;

namespace Pay.Service.AutoMapper {

    public class ShopInfoMapper : IStartupDto {
        private IMapperConfigurationExpression _cfg;
        private IRegionService _iRegionService;
        public ShopInfoMapper(IMapperConfigurationExpression cfg) {
            _cfg = cfg;
            _iRegionService = IocHelper.Get<IRegionService>();
        }
        public void Execute() {
            Create();
        }
        protected virtual void Create() {
            _cfg.CreateMap<ShopInfo, shop_info_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.red_drill_receiver_id, src => src.MapFrom(c => c.RedDrillReceiverId))
                .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                .ForMember(dest => dest.customer_tel, src => src.MapFrom(c => c.CustomerTel))
                .ForMember(dest => dest.shop_name, src => src.MapFrom(c => c.ShopName))
                .ForMember(dest => dest.shop_address, src => src.MapFrom(c => c.ShopAddress))
                .ForMember(dest => dest.region_id, src => src.MapFrom(c => c.RegionId))
                .ForMember(dest => dest.region_name, src => src.MapFrom(c => c.RegionName))
                .ForMember(dest => dest.shop_logo, src => src.MapFrom(c => c.ShopLogo))
                .ForMember(dest => dest.shop_background_img, src => src.MapFrom(c => c.ShopBackgroundImg))
                .ForMember(dest => dest.entrepot_id, src => src.MapFrom(c => c.EntrepotId))
                .ForMember(dest => dest.is_disabled, src => src.MapFrom(c => c.IsDisabled))
                .ForMember(dest => dest.remark, src => src.MapFrom(c => c.Remark)).AfterMap((src, dest) => {
                    if (src.RegionId > 0) {
                        var ids = _iRegionService.GetRegionIds(src.RegionId);
                        if (ids.Length >= 2) {
                            dest.province_id = ids[0];
                            dest.city_id = ids[1];
                            if (ids.Length > 2)
                                dest.area_id = ids[2];
                        }
                        dest.region_name = _iRegionService.GetRegionNames(src.RegionId);
                        dest.remark=string.IsNullOrEmpty(src.Remark)?src.Remark:src.Remark.Replace("\n", "").Replace("\r", "").Replace("\t", "");
                    }
                });
            _cfg.CreateMap<shop_info_dto, ShopInfo>()
                .ForMember(dest => dest.Id, src => src.MapFrom(c => c.id))
                .ForMember(dest => dest.RedDrillReceiverId, src => src.MapFrom(c => c.red_drill_receiver_id))
                .ForMember(dest => dest.CreateTime, src => src.MapFrom(c => c.create_time.ToUnix()))
                .ForMember(dest => dest.CustomerTel, src => src.MapFrom(c => c.customer_tel))
                .ForMember(dest => dest.ShopLogo, src => src.MapFrom(c => c.shop_logo))
                .ForMember(dest => dest.ShopName, src => src.MapFrom(c => c.shop_name))
                .ForMember(dest => dest.RegionId, src => src.MapFrom(c => c.region_id))
                .ForMember(dest => dest.RegionName, src => src.MapFrom(c => c.region_name))
                .ForMember(dest => dest.Remark, src => src.MapFrom(c => c.remark))
                .ForMember(dest => dest.ShopBackgroundImg, src => src.MapFrom(c => c.shop_background_img))
                .ForMember(dest => dest.EntrepotId, src => src.MapFrom(c => c.entrepot_id))
                .ForMember(dest => dest.ShopAddress, src => src.MapFrom(c => c.shop_address))
                .ForMember(dest => dest.FirstCategoriesId, src => src.MapFrom(c => c.first_categories_id))
                .ForMember(dest => dest.FirstCategoriesName, src => src.MapFrom(c => c.first_categories_name))
                .ForMember(dest => dest.SecondCategoriesId, src => src.MapFrom(c => c.second_categories_id))
                .ForMember(dest => dest.SecondCategoriesName, src => src.MapFrom(c => c.second_categories_name));



            _cfg.CreateMap<ShopInfo, shop_info_dto_ex>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.red_drill_receiver_id, src => src.MapFrom(c => c.RedDrillReceiverId))
                 .ForMember(dest => dest.rate, src => src.MapFrom(c => c.Rate))
                .ForMember(dest => dest.create_time, src => src.MapFrom(c => c.CreateTime.ToDateTime()))
                .ForMember(dest => dest.customer_tel, src => src.MapFrom(c => c.CustomerTel))
                .ForMember(dest => dest.shop_name, src => src.MapFrom(c => c.ShopName))
                .ForMember(dest => dest.shop_address, src => src.MapFrom(c => c.ShopAddress))
                .ForMember(dest => dest.region_id, src => src.MapFrom(c => c.RegionId))
                .ForMember(dest => dest.region_name, src => src.MapFrom(c => c.RegionName))
                .ForMember(dest => dest.shop_logo, src => src.MapFrom(c => c.ShopLogo))
                .ForMember(dest => dest.shop_background_img, src => src.MapFrom(c => c.ShopBackgroundImg))
                .ForMember(dest => dest.is_disabled, src => src.MapFrom(c => c.IsDisabled))
                .ForMember(dest => dest.entrepot_id, src => src.MapFrom(c => c.EntrepotId))
                .ForMember(dest => dest.merchant, src => src.MapFrom(c => c.Merchant))
                .ForMember(dest => dest.remark, src => src.MapFrom(c => c.Remark)).AfterMap((src, dest) => {
                    if (src.RegionId > 0) {
                        var ids = _iRegionService.GetRegionIds(src.RegionId);
                        dest.province_id = ids[0];
                        dest.city_id = ids[1];
                        if (ids.Length > 2)
                            dest.area_id = ids[2];
                        dest.region_name = _iRegionService.GetRegionNames(src.RegionId);
                    }
                });
        }
    }
}
